# -*- coding: utf-8 -*-
# @Author  : gaoyu
# @Time    : 2024/3/2 11:08 AM
# @Function:
import os
import re

import pandas as pd
from loguru import logger

from util.df_util import dict_to_df


def get_files(file_path):
    return os.listdir(file_path)


def get_sub_dir(directory):
    """
    取当前目录和上级目录
    :param directory:
    :return:
    """
    directory = re.sub("/$", "", directory)
    directory, sub_directory = os.path.split(directory)
    return directory + "/", sub_directory


def get_upper_dir(directory):
    """
    取当前目录和上级目录
    :param directory:
    :return:
    """
    directory = re.sub("/$", "", directory)
    directory, sub_directory = os.path.split(directory)
    return directory


def get_dir_filename(directory):
    """
    取目录和文件夹
    :param directory:
    :return:
    """
    return os.path.split(directory)


def get_abs_file_path(_file_path):
    """
    获取绝对路径
    :param _file_path:
    :return:
    """
    abs_file_path = os.path.abspath(_file_path)
    return abs_file_path


def remove_folder(_folder_path):
    """
    删除文件夹
    :param _folder_path: 文件夹路径
    :return:
    """
    if os.path.exists(_folder_path):
        if os.path.isfile(_folder_path) or os.path.islink(_folder_path):
            os.remove(_folder_path)
        else:
            for filename in os.listdir(_folder_path):
                remove_folder(os.path.join(_folder_path, filename))
            os.rmdir(_folder_path)
    logger.info("文件夹" + _folder_path + "删除完成")


def list_dir(file_path):
    """
    列出当前文件夹
    :param file_path:
    :return:
    """
    return [dI for dI in os.listdir(file_path) if os.path.isdir(os.path.join(file_path, dI))]


def mkdir(dirs):
    """
    目录不存在就创建
    :param dirs:
    :return:
    """
    if not os.path.exists(dirs):
        os.makedirs(dirs)


def recursive_listdir(path, suffix):
    """
    递归遍历路径
    :param path: 路径
    :param suffix: 后缀名
    :return:
    """
    result_df = pd.DataFrame()

    files = os.listdir(path)
    for file in files:
        file_path = os.path.join(path, file)
        if os.path.isfile(file_path) and file_path.endswith(suffix):
            temp_df = dict_to_df(
                {
                    'file_path': path,
                    'file': file,
                    'abs_file_path': get_abs_file_path(file_path + file)
                }
            )
            result_df = pd.concat(
                [
                    result_df,
                    temp_df
                ]
            )
        elif os.path.isdir(file_path):
            temp_df = recursive_listdir(file_path, suffix)
            result_df = pd.concat(
                [
                    result_df,
                    temp_df
                ]
            )
    return result_df


if __name__ == '__main__':
    # print(get_sub_dir("/home/xuehua/Data deal/sx/03_ob_zq/stations_list"))
    # print(get_sub_dir("/home/xuehua/Data deal/sx/03_ob_zq/stations_list/111.csv"))

    # _file_path, _sub_directory = get_sub_dir("/home/xuehua/Data deal/sx/03_ob_zq/stations_list/111.csv")
    # print(_file_path)
    # get_sub_dir(get_abs_file_path(_file_path))

    # print(get_dir_filename("/Users/gaoyu/sourceCode/dalianwaterbureau_dataanalysis/data/小库/st_pptn_r.csv"))

    # filename = '/Users/gaoyu/sourceCode/water-storage/data/xuehua/reservoirs-origin/day_data'
    # _file_path, _sub_directory = get_sub_dir(filename)
    # print(_file_path)
    # output = [dI for dI in os.listdir(_file_path) if os.path.isdir(os.path.join(_file_path, dI))]
    # print(output)

    df = recursive_listdir("../", ".py")
    print(df)
